package com.hitqz.robot.biz.flow.domain.dto;

import cn.hutool.core.collection.CollUtil;
import com.yomahub.liteflow.flow.element.Chain;
import com.yomahub.liteflow.flow.element.Condition;
import com.yomahub.liteflow.flow.element.Executable;
import com.yomahub.liteflow.flow.element.Node;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 节点dto
 *
 * @author wengyx
 * @date 2023/08/23
 */
@Data
@Builder
@Slf4j
public class NodeDto implements Serializable {
    @Serial
    private static final long serialVersionUID = 5474717056994112085L;
    private String id;
    private String name;
    private String type;

    public static List<NodeDto> getChainNodes(Chain chain) {
        List<NodeDto> list = new ArrayList<>();
        for (Condition condition : CollUtil.emptyIfNull(chain.getConditionList())) {
            for (Executable executable : CollUtil.emptyIfNull(condition.getExecutableList())) {
                if (executable instanceof Node) {
                    Node node = (Node) executable;
                    list.add(NodeDto.builder().id(node.getId()).name(node.getName()).type(node.getType().name()).build());
                }else {
                    log.warn("对象不是 Node 类型: " + executable.getClass().getName());
                }
            }
        }
        return list;
    }

    public static NodeDto convertNode2Dto(Node node) {
        return NodeDto.builder().id(node.getId()).name(node.getName()).type(node.getType().name()).build();
    }

}
